/* This file is part of HealthyFood.

   HealthyFood is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   HealthyFood is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with HealthyFood.  If not, see <http://www.gnu.org/licenses/>.

	Portions of this file are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 *	copyright (c) 2009 Vytautas Kasparavičius
 */

package com.healthyFood;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; //import com.google.zxing.*;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.view.Menu;
import android.view.MenuItem;

public class HealthyFood extends Activity {
	private DbAdapter mDbHelper;
	private Long barcode; // used by AlertDialog(s)

	private static final int NOT_FOUND_DIALOG = 0;

	private static final int VIEW_ITEMS_MENU = 0;
	private static final int SYNCHRONIZE_MENU = 1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mDbHelper = new DbAdapter(this);
		mDbHelper.open();

		findViewById(R.id.getFromCameraBtn).setOnClickListener(lGetFromCamera);
		findViewById(R.id.getFromKeyboardBtn).setOnClickListener(
				lGetFromKeyboard);
	}

	public final Button.OnClickListener lGetFromCamera = new Button.OnClickListener() {
		public void onClick(View v) {
			Intent intent = new Intent("com.google.zxing.client.android.SCAN");
			intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
			startActivityForResult(intent, 0);
		}
	};

	public final Button.OnClickListener lGetFromKeyboard = new Button.OnClickListener() {
		public void onClick(View v) {
			Intent i = new Intent(HealthyFood.this,
					com.healthyFood.ProductEdit.class);
			startActivityForResult(i, 1);
		}
	};

	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		if (requestCode == 0) {
			if (resultCode == RESULT_OK) {
				String contents = intent.getStringExtra("SCAN_RESULT");
				search(contents);
			} else if (resultCode == RESULT_CANCELED) {
				// do nothing
			}
		}
	}

	public void search(String barcode) {
		try {
			search(Long.parseLong(barcode));
		} catch (NumberFormatException e) {
			new AlertDialog.Builder(HealthyFood.this).setTitle(
					getString(R.string.errorWhileConverting)).setMessage(
					getString(R.string.errorDescBadBarcode)).setPositiveButton(
					"OK", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
						}
					}).create().show();
			return;
		}
	}

	public void search(final Long barcode) {
		try {
			Cursor product = mDbHelper.fetchProduct(barcode);
			startManagingCursor(product);

			String code = product.getString(product
					.getColumnIndexOrThrow(DbAdapter.KEY_BARCODE));
			String title = product.getString(product
					.getColumnIndexOrThrow(DbAdapter.KEY_TITLE));
			String contents = product.getString(product
					.getColumnIndexOrThrow(DbAdapter.KEY_CONTENTS));

			new AlertDialog.Builder(HealthyFood.this).setTitle("Found item")
					.setMessage(
							"barcode: " + code + ", title: " + title
									+ ", contents:" + contents)
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
								}
							}).create().show();
		} catch (SQLException sqlE) {
		} catch (Exception e) { // if product is not found
			this.barcode = barcode;
			showDialog(HealthyFood.NOT_FOUND_DIALOG);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case HealthyFood.NOT_FOUND_DIALOG:
			return new AlertDialog.Builder(HealthyFood.this).setTitle(
					getString(R.string.errorWhileConverting)).setMessage(
					getString(R.string.errorDescBadBarcode)).setNegativeButton(
					"Cancel", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
						}
					}).setPositiveButton("Add",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							Intent i = new Intent(HealthyFood.this,
									com.healthyFood.ProductEdit.class);
							i.putExtra("barcode", barcode);
							startActivityForResult(i, 0);
						}
					}).create();
		}
		return null;
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, HealthyFood.VIEW_ITEMS_MENU, 0,
				getString(R.string.viewItemsMenu_HealthyFood)).setIcon(R.drawable.list);
		menu.add(0, HealthyFood.SYNCHRONIZE_MENU, 0,
				getString(R.string.synchronizeMenu_HealthyFood)).setIcon(R.drawable.synchonize);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case HealthyFood.VIEW_ITEMS_MENU:
			// newGame();
			Intent i = new Intent(HealthyFood.this, com.healthyFood.ProductListing.class);
			startActivityForResult(i, 0);
			return true;
		case HealthyFood.SYNCHRONIZE_MENU:
			// quit();
			return true;
		}

		return false;
	}

}